<template>
  <div>
    <div>
      <el-input
          style="width: 300px; margin-right: 10px"
          prefix-icon="el-icon-search"
          v-model="patientName"
          @keydown.enter.native="initPatient"
          clearable
          @clear="initPatient"
          @input="initPatient"
          placeholder="请输入病人姓名进行搜索"
      ></el-input>
      <el-button
          type="primary"
          icon="el-icon-search"
          @click="initPatient">搜索
      </el-button>
      <el-button type="primary" icon="el-icon-plus" @click="showAddPatient">添加病人</el-button>
      <el-table :data="patient" stripe border style="width: 100%;margin-top: 5px"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="name" label="姓名" width="80" fixed></el-table-column>
        <el-table-column prop="gender" label="性别" width="70"></el-table-column>
        <el-table-column prop="birthday" label="出生日期" width="100"></el-table-column>
        <el-table-column prop="idCard" label="身份证" width="100"></el-table-column>
        <el-table-column prop="nativePlace" label="籍贯" width="90"></el-table-column>
        <el-table-column prop="phone" label="家庭号码" width="100"></el-table-column>
        <el-table-column prop="address" label="家庭住址" width="100"></el-table-column>
        <el-table-column prop="illness" label="病因" width="100"></el-table-column>
        <el-table-column prop="isLeaveHospital" label="是否住院"></el-table-column>
        <el-table-column prop="nation.name" label="民族" width="70"></el-table-column>
        <el-table-column prop="politicsStatus.name" label="政治面貌"></el-table-column>
        <el-table-column prop="roomType.name" label="病房类型"></el-table-column>
        <el-table-column prop="medicineInfo.medicineName" label="用药信息"></el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template v-slot="scope">
            <el-button
                style="padding: 3px"
                size="mini"
                @click="showEditPatientView(scope.row)">编辑
            </el-button>
            <template>
              <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @confirm="confirmDelete(scope.row)"
              >
                <el-button style="padding: 3px"
                           size="mini" slot="reference" type="danger">删除
                </el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 20px ;display: flex;justify-content: space-between;margin-top: 5px">
        <span>
         <el-button
             size="mini"
             type="danger"
             :disabled="this.multipleSelection.length == 0"
             @click="batchDelete">批量删除
      </el-button>
      </span>
        <span style="display: flex; justify-content: flex-end">
        <el-pagination
            background
            @current-change="currentChange"
            @size-change="sizeChange"
            layout=" sizes,prev, pager, next, jumper, ->, total, slot"
            :total="total"
        >
        </el-pagination>
     </span>
      </div>
      <el-form ref="patientForm" :model="pForm" :rules="rules">
        <el-dialog :title="patientDialog" :visible.sync="dialogVisible" width="80%">
          <el-row>
            <el-col :span="6">
              <el-form-item label="姓名：" prop="name">
                <el-input
                    size="mini"
                    style="width: 150px"
                    prefix-icon="el-icon-edit"
                    v-model="pForm.name"
                    placeholder="请输入病人姓名"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="身份证号：" prop="idCard">
                <el-input
                    size="mini"
                    style="width: 150px"
                    prefix-icon="el-icon-edit"
                    v-model="pForm.idCard"
                    placeholder="请输入身份证号"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="性别：" prop="gender" style="margin-left: 40px">
                <el-radio-group v-model="pForm.gender" style="margin-left: -10px">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="6" style="margin-left: 40px">
              <el-form-item label="出生日期：" prop="birthday">
                <el-date-picker
                    v-model="pForm.birthday"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="出生日期"
                    size="mini"
                    style="width: 140px"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="7">
              <el-form-item label="政治面貌：" prop="politicId">
                <el-select
                    v-model="pForm.politicId"
                    size="mini"
                    style="width: 170px"
                    placeholder="政治面貌"
                >
                  <el-option
                      v-for="item in politicsstatus"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="民族：" prop="nationId">
                <el-select
                    v-model="pForm.nationId"
                    size="mini"
                    style="width: 150px"
                    placeholder="民族"
                >
                  <el-option
                      v-for="item in nations"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="籍贯：" prop="nativePlace">
                <el-input
                    v-model="pForm.nativePlace"
                    placeholder="请输入籍贯"
                    prefix-icon="el-icon-edit"
                    size="mini"
                    style="width: 110px"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="家庭电话：" prop="phone">
                <el-input
                    v-model="pForm.phone"
                    placeholder="请输入电话号码"
                    size="mini"
                    style="width: 140px"
                    prefix-icon="el-icon-phone"
                ></el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="7">
              <el-form-item label="联系地址：" prop="address">
                <el-input
                    v-model="pForm.address"
                    placeholder="请输入地址"
                    size="mini"
                    style="width: 170px"
                    prefix-icon="el-icon-location-outline"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="病因：" prop="illness">
                <el-input
                    v-model="pForm.illness"
                    size="mini"
                    style="width: 150px">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="9">
              <el-form-item label="是否住院：" prop="isLeaveHospital">
                <el-radio-group v-model="pForm.isLeaveHospital">
                  <el-radio label="未住院">未住院</el-radio>
                  <el-radio label="已住院">已住院</el-radio>
                  <el-radio label="已出院">已出院</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="房间类型：" prop=" wardRoomId">
                <el-select
                    v-model="pForm.wardRoomId"
                    size="mini"
                    style="width: 150px"
                    placeholder="房间类型"
                >
                  <el-option
                      v-for="item in roomTypes"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="6">
            <el-form-item label="用药信息：" prop=" medicineId">
              <el-select
                  v-model="pForm.medicineId"
                  size="mini"
                  style="width: 150px"
                  placeholder="用药信息"
              >
                <el-option
                    v-for="item in medicineInfos"
                    :key="item.id"
                    :label="item. medicineName"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doAddPatient">确 定</el-button>
      </span>
        </el-dialog>
      </el-form>
    </div>
  </div>

</template>

<script>
export default {
  name: 'EmpAdv',
  data() {
    return {
      patient: [],
      total: 0,
      pageCurrent: 1,
      pageSize: 10,
      patientName: '',
      dialogVisible: false,
      patientDialog: '',
      pForm: {
        id: null,
        name: '',
        gender: '',
        birthday: '',
        idCard: '',
        nationId: null,
        nativePlace: '',
        politicId: null,
        phone: '',
        address: '',
        illness: '',
        isLeaveHospital: '',
        wardRoomId: null,
        medicineId: null,
        medicineInfo:'',
        isAccounts:'',
        roomPrice:'',
        medicinePrice: '',
        allDetails: '',
      },
      medicineName:'',
      medicineInfos: [],
      multipleSelection: [],
      roomTypes: [],
      politicsstatus: [],
      nations: [],
      rules: {
        name: [{required: true, message: '病人姓名不能为空', trigger: 'blur'}],
        gender: [{required: true, message: '病人性别不能为空', trigger: 'blur'}],
        birthday: [{required: true, message: '病人出生日期不能为空', trigger: 'blur'}],
        idCard: [
          {
            required: true,
            message: '员工身份证号码不能为空',
            trigger: 'blur',
          },
          {
            pattern:
                /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
            message: '身份证号码格式不正确',
            trigger: 'blur',
          },
        ],
        nationId: [{required: true, message: '病人民族不能为空', trigger: 'blur'}],
        nativePlace: [{required: true, message: '病人籍贯不能为空', trigger: 'blur'}],
        politicId: [{required: true, message: '病人政治面貌不能为空', trigger: 'blur'}],
        phone: [{required: true, message: '病人联系电话号码不能为空', trigger: 'blur'}],
        address: [{required: true, message: '病人家庭住址不能为空', trigger: 'blur'}],
        illness: [{required: true, message: '病因不能为空', trigger: 'blur'}],
        isLeaveHospital: [{required: true, message: '是否住院不能为空', trigger: 'blur'}],
        wardRoomId: [{required: true, message: '房间类型不能为空', trigger: 'blur'}],
        medicineId: [{required: true, message: '用药信息不能为空', trigger: 'blur'}]
      },

    }
  },
  methods: {
    initPatient() {
      this.getRequest('/patient/?pageCurrent=' + this.pageCurrent + '&pageSize=' + this.pageSize + '&name=' + this.patientName).then((resp) => {
        if (resp) {
          this.patient = resp.data;
          this.total = resp.total;
        }
      })
    },
    currentChange(currentPage) {
      this.pageCurrent = currentPage;
      this.initPatient();
    },
    sizeChange(pageSize) {
      this.pageSize = pageSize;
      this.initPatient();
    },
    showEditPatientView(data) {
      this.patientDialog = '编辑病人信息';
      this.pForm = data
      // this.pForm.wardRoomId = data.roomType.name
      // this.pForm.medicineId=data.medicineInfo.medicineName
      this.dialogVisible = true;
    },
    showAddPatient() {
      this.patientDialog = "添加病人信息";
      this.pForm = {
        id: null,
        name: '',
        gender: '',
        birthday: '',
        idCard: '',
        nationId: null,
        nativePlace: '',
        politicId: null,
        phone: '',
        address: '',
        illness: '',
        isLeaveHospital: '',
        roomTypes: '',
        medicineId: null,
      }
      this.dialogVisible = true;
    },
    confirmDelete(data) {
      this.$confirm(
          '将永久删除该病人信息:' + data.name + ', 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      )
          .then(() => {
            this.deleteRequest('/patient/' + data.id).then((resp) => {
              if (resp) {
                this.initPatient()
              }
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    doAddPatient() {
      if (this.pForm.id) {
        this.$refs['patientForm'].validate((valid) => {
          if (valid) {
            this.putRequest('/patient/', this.pForm).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initPatient()
              }
            })
          }
        })
      } else {
        this.$refs['patientForm'].validate((valid) => {
          if (valid) {
            this.postRequest('/patient/', this.pForm).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initPatient()
              }
            })
          }
        })
      }
    },
    initDate() {
      this.nations = JSON.parse(window.sessionStorage.getItem('nations'))
      this.politicsstatus = JSON.parse(window.sessionStorage.getItem('politicsstatus'))

      if (!window.sessionStorage.getItem('wardRoomType')) {
        this.getRequest('/patient/wardRoomType/').then((resp) => {
          if (resp) {
            this.roomTypes = resp
            window.sessionStorage.setItem('wardRoomType', JSON.stringify(resp))
          }
        })
      } else {
        this.roomTypes = JSON.parse(
            window.sessionStorage.getItem('wardRoomType')
        )
      }

      if (!window.sessionStorage.getItem('medicine')) {
        this.getRequest('/medicine/name/').then((resp) => {
          if (resp) {
            this.medicineInfos = resp
            window.sessionStorage.setItem('medicine', JSON.stringify(resp))
          }
        })
      } else {
        this.medicineInfos = JSON.parse(
            window.sessionStorage.getItem('medicine')
        )
      }
    },
    batchDelete() {
      this.$confirm(
          '此操作将删[' + this.multipleSelection.length + ']条数据, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      )
          .then(() => {
            let ids = '?'
            this.multipleSelection.forEach((item) => {
              ids += 'ids=' + item.id + '&'
            })
            this.deleteRequest('/patient/' + ids).then((resp) => {
              if (resp) {
                this.initPatient()
              }
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
    }
  },
  mounted() {
    this.initPatient()
    this.initDate()
  },
}
</script>

<style>

</style>